home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / MacApp Documentation / MacApp AppleLink Messages / MacApp.Tech$ Oct 89 / Z0133-Re Object Casting…-Oct89 < prev    next >
Encoding:
Text File  |  1989-10-20  |  2.9 KB  |  84 lines  |  [TEXT/GEOL]

  1. Item    3102473                         19-Oct-89        09:38
  2.  
  3. From:   SCHMUCKER1                      Schmucker, Kurt
  4.  
  5. To:     D0220                           American Zettler, Dirk Tjossen,PRT
  6.  
  7. cc:     MACAPP.TECH$                    MACAPP Tech
  8.         DEREK                           White, Derek
  9.  
  10. Sub:    Re: Object Casting…
  11.  
  12. Dirk,
  13.  
  14.     The object casting issue you bring up has to do with the REFERENCE Type of
  15. an object (what the compiler thinks it is) and the ACTUAL type (what it really
  16. is).  These are sometimes the same, but not always.  One case where they are
  17. usually different is in Lists.
  18.  
  19.     Suppose that we have the following classes:
  20.  
  21.     TMammal = OBJECT (TObject)
  22.         Procedure TMammal.Eat;
  23.     End;
  24.  
  25.     TCat = OBJECT (TMammal)
  26.         Procedure TCat.Eat; OVERRIDE;
  27.         Procedure TCat.Meow;
  28.     End;
  29.  
  30.     TDog = OBJECT (TMammal)
  31.         Procedure TDog.Eat; OVERRIDE;
  32.         Procedure TDog.Bark;
  33.     End;
  34.  
  35. and further suppose that aDog, aCat, and a Mammal are three instances.
  36.  
  37.     Then the compiler will do the right thing if we say
  38.  
  39.         aDog.Eat     (TDog.Eat will be called)
  40.         aCat.Eat     (TCat.Eat will be called)
  41.         aDog.Bark    (OK)
  42.         aCat.Meow    (OK)
  43.  
  44.  
  45. However, if we do this, there will be SOME problems:
  46.  
  47.         aMammal := aDog;    (anObject is now REALLY a TDog.  That is, its
  48.                              actual type is TDog.  Its reference type is still
  49.                              TMammal.  The reference type of an object is
  50.                              fixed at compile time.  The actual type may vary
  51.                              during the execution of the program.)
  52.  
  53.         aMammal.Eat;        (No problem.  TDog.Eat will be called.  The actual
  54.                              type, i.e., the run-time type, is used for message
  55.                              dispatching.)
  56.  
  57.         aMammal.Bark;       (Problem.  Since the reference type is TMammal and
  58.                              since mammals don't have a Bark method, the
  59.                              compiler won't compile this for you, EVEN THOUGH
  60.                              would be OK at run-time.  The compiler doesn't
  61.                              know this and thus would not let you put yourself
  62.                              into a situation where there COULD be a run-time
  63.                              error.)
  64.  
  65.         TDog(aMammal).Bark; (OK)
  66.  
  67.  
  68.     In Object Pascal, lists are usually type homogeneous, and usually the
  69. reference type of the elements in the list is TObject, even if the actual type
  70. is TView or something.  Thus, when you pull something out of a list, the
  71. compiler won't let you send it any message not defined for TObject, without the
  72. additional assurance from you of what the actual type really is.  This
  73. assurance takes the form of a type cast, like this:
  74.  
  75.  
  76.     TDog(myAnimalList.FirstThat(FooBar)).Bark;      (OK)
  77.  
  78.  
  79.  
  80.         Hope this helps you over one of Object Pascal's trickier parts,
  81.  
  82.                     Kurt
  83.  
  84.